package com.localjoycommunity.cpdj.project.system.service.impl;

import java.util.List;

import com.localjoycommunity.cpdj.common.utils.DateUtils;
import com.localjoycommunity.cpdj.project.system.domain.SysPaper;
import com.localjoycommunity.cpdj.project.system.domain.UserActivity;
import com.localjoycommunity.cpdj.project.system.mapper.SysPaperMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.localjoycommunity.cpdj.project.system.mapper.UserAnswerMapper;
import com.localjoycommunity.cpdj.project.system.domain.UserAnswer;
import com.localjoycommunity.cpdj.project.system.service.IUserAnswerService;

/**
 * 用户答题记录Service业务层处理
 *
 * @author leepandar
 * @date 2020-11-16
 */
@Service
public class UserAnswerServiceImpl implements IUserAnswerService {
    @Autowired
    private UserAnswerMapper userAnswerMapper;
    @Autowired
    private SysPaperMapper sysPaperMapper;

    /**
     * 查询用户答题记录
     *
     * @param id 用户答题记录ID
     * @return 用户答题记录
     */
    @Override
    public UserAnswer selectUserAnswerById(Long id) {
        return userAnswerMapper.selectUserAnswerById(id);
    }

    /**
     * 查询用户答题记录列表
     *
     * @param userAnswer 用户答题记录
     * @return 用户答题记录
     */
    @Override
    public List<UserAnswer> selectUserAnswerList(UserAnswer userAnswer) {
        return userAnswerMapper.selectUserAnswerList(userAnswer);
    }

    /**
     * 新增用户答题记录
     *
     * @param userAnswer 用户答题记录
     * @return 结果
     */
    @Override
    public int insertUserAnswer(UserAnswer userAnswer) {
        userAnswer.setCreateTime(DateUtils.getNowDate());
        userAnswerMapper.insertUserAnswer(userAnswer);
        Long paperId = userAnswer.getPaperId();
        SysPaper sysPaper = sysPaperMapper.selectSysPaperById(paperId);
        UserAnswer ua = new UserAnswer();
        ua.setPaperId(paperId);
        List<UserAnswer> userAnswerList = userAnswerMapper.selectUserAnswerList(ua);
        Long correct = 0L;
        for (UserAnswer answer : userAnswerList) {
            correct += answer.getCorrectPercent();
        }
        int size;
        if (userAnswerList == null || userAnswerList.size() == 0) {
            size = 1;
        } else {
            size = userAnswerList.size();
        }
        String correctPercent = String.valueOf(correct / size) + "%";
        sysPaper.setCorrectPercent(correctPercent);
        sysPaper.setAnswerNum(sysPaper.getAnswerNum() + 1);
        return sysPaperMapper.updateSysPaper(sysPaper);
    }

    /**
     * 修改用户答题记录
     *
     * @param userAnswer 用户答题记录
     * @return 结果
     */
    @Override
    public int updateUserAnswer(UserAnswer userAnswer) {
        return userAnswerMapper.updateUserAnswer(userAnswer);
    }

    /**
     * 批量删除用户答题记录
     *
     * @param ids 需要删除的用户答题记录ID
     * @return 结果
     */
    @Override
    public int deleteUserAnswerByIds(Long[] ids) {
        return userAnswerMapper.deleteUserAnswerByIds(ids);
    }

    /**
     * 删除用户答题记录信息
     *
     * @param id 用户答题记录ID
     * @return 结果
     */
    @Override
    public int deleteUserAnswerById(Long id) {
        return userAnswerMapper.deleteUserAnswerById(id);
    }
}
